๐ How LLM Serving Works: vLLM & SGLang โ
Notes from the lecture How LLM Serving Works: An Introduction to vLLM and SGLang by Dr. Sreedath Panat (Vizuara).
๐งญ The whole lecture in one line
A GPU is very fast at maths but slow at moving data. vLLM and SGLang are clever "managers" that let one GPU serve many users at once without wasting memory. Result: the same 1 million users cost about 6ร less.
We follow one example the whole way, so the numbers stay simple:
| Thing | Value |
|---|---|
| ๐ค Model | Llama 3.1 8B (8 billion numbers called weights) |
| ๐ฅ๏ธ GPU | One NVIDIA H100 (80 GB memory) |
| ๐ต GPU rent | about $2.5 per hour |
1. ๐ธ Why serving matters more than training โ
Think of a restaurant ๐ฝ๏ธ:
- Training = building the kitchen. Very expensive, but you pay once.
- Serving (inference) = cooking every meal for every customer. Cheap per meal, but you pay every day, forever.
Flat line = training (one-time). Rising line = serving (adds up every month). Sooner or later, serving costs more.
That's why so many companies work on making inference cheaper and faster.
๐ How much work is "1 million users"? โ
And this is a light user. People using coding agents easily burn 200,000 tokens a day.
2. ๐งฐ What are vLLM and SGLang? โ
A trained model is just a big file of numbers. Someone has to actually run it: multiply the numbers with your input, layer by layer, to get the next word. That "someone" is an inference engine.
| vLLM | SGLang | |
|---|---|---|
| Full name | No official one (people say "virtual") | Started as Structured Generation Language |
| Came from | UC Berkeley | Also has a UC Berkeley team |
| Famous idea | PagedAttention | RadixAttention |
| Used for | Serving open models (Llama, Mistral, DeepSeekโฆ) to many users | Same |
In one sentence: vLLM and SGLang are engines that run an LLM for thousands of users at the same time, efficiently.
๐ What about llama.cpp? That's for one person on one device (your laptop or phone). vLLM/SGLang are for many people on a server.
3. ๐ฅ๏ธ Inside a GPU: the warehouse and the workbench โ
A GPU has two kinds of memory. The easiest way to picture it:
- The warehouse (VRAM) is big, so the whole model lives there.
- The workbench (SRAM) is where the maths happens, but it is tiny: 30 MB. Even one layer of the model (~500 MB) doesn't fit!
- So the GPU keeps carrying small pieces of the model from the warehouse to the workbench, doing the maths, and sending the result back.
Why does Llama 8B take 16 GB? โ
Big frontier models may have trillions of weights, so they need thousands of GB. That's why we use a small model in this example.
โก The big surprise: maths is fast, carrying is slow โ
To make one new token, the GPU must carry all 16 GB of weights to the workbench once.
| Step | Calculation | Time |
|---|---|---|
| ๐ Carry weights | 16 GB รท 3.35 TB/s | โ 4.8 ms |
| โ๏ธ Do the maths | 16 billion operations รท 989 trillion per second | โ 0.016 ms |
๐ข Carrying takes about 300ร longer than the maths. We say serving is memory-bound: the road is the bottleneck, not the workers.
So one user alone gets about 1 รท 4.8 ms โ 200 tokens per second, and the cores sit mostly idle. Keep this in mind; it's the key to everything that follows.
4. ๐โ๏ธ Prefill vs decode: reading vs writing โ
An LLM writes one token at a time, then adds it to the input and goes again:
There are two phases, and they behave very differently:
| ๐ Prefill | โ๏ธ Decode | |
|---|---|---|
| What | Reading your question | Writing the answer |
| How | All tokens at once | One token at a time |
| Why | You already gave all the words | Can't write word 5 before word 4 exists |
| Feels like | Very fast | The "typing" you watch |
๐ก Try it: paste a huge document into ChatGPT and ask a question. The answer starts almost instantly, because the whole document is "read" in parallel during prefill.
โฑ๏ธ What the user actually feels โ
That's why apps stream words as they come: you only "feel" the wait until the first token (TTFT), not the whole answer.
Typical speeds: hundreds of tokens/s on a server GPU is good. A small model on a phone gets about 18โ30 tokens/s.
5. ๐๏ธ The KV cache: the model's notebook โ
A tiny bit of attention (no scary maths) โ
Each word is turned into three vectors (lists of numbers):
| Vector | Simple meaning |
|---|---|
| Q (query) | "What am I looking for?" |
| K (key) | "What do I contain?" (a label) |
| V (value) | "Here is my actual information" |
To make the next word, the newest word's Q is compared with the K of every earlier word. Those scores decide how much of each earlier word's V to mix in.
โ Why save K and V, but not Q? โ
- Q is only needed for the newest word. Once used, throw it away. ๐๏ธ
- K and V of every old word are needed again every single round.
So we save K and V in a notebook = the KV cache. Without it, the model would re-read the whole conversation for every new word.
๐งฎ How big is the notebook? โ
For Llama 3.1 8B, per token:
Why only 8 KV heads? Llama uses grouped-query attention: 4 query heads share one set of keys and values, which shrinks the cache 4ร.
| Context size | KV cache for one user |
|---|---|
| 1 token | 128 KB |
| 2,500 tokens (~4 pages) | โ 320 MB |
๐ง How many users fit on one H100? โ
54 GB รท 0.32 GB per user โ 160 users (at 2,500 tokens each).
โ ๏ธ If users want longer chats (more pages), each user's notebook grows and fewer users fit. This is the core problem vLLM and SGLang attack.
6. ๐ Batching: fill the van โ
Remember: carrying weights is the slow part. So once the weights are on the workbench, use them for everyone!
It's like a delivery van ๐: the drive costs the same whether it carries 1 parcel or 100. So fill it up.
| Users per GPU | Tokens per second (whole GPU) | |
|---|---|---|
| One user only | 1 | ~200 |
| Batched | 64โ128 | ~8,000 |
Each user waits only a little longer, but the GPU does 40ร more work. Each user also gets their own next token, even if one is on word 2 and another on word 2,342.
7. ๐๏ธ What's inside one inference engine โ
A company has many GPUs. Each one runs a copy (replica) of the model with vLLM or SGLang.
๐ฏ Cache-aware routing โ
You asked a question and Replica 1 answered. Now you ask a follow-up. Where should it go?
A smart router remembers where your notebook lives and sends you back there. This is cache-aware routing.
๐งฑ Who does what? โ
- Inference engineer: works on the vLLM/SGLang layer (scheduling, memory).
- Kernel engineer: works one layer below (making the maths on the cores fast).
8. ๐ Trick #1: PagedAttention (vLLM) โ
The problem: booking seats you never use โ
Old servers reserved a big, continuous chunk of memory per user, sized for the longest possible answer. But nobody knows how long an answer will be.
- "What's the capital of France? One word." โ needs a tiny notebook.
- "Explain this 5-page document." โ needs a huge one.
The fix: small pages, placed anywhere โ
Like a computer's operating system (virtual memory), split memory into small pages. In this example a page holds 16 tokens โ 2 MB.
- Get a new page only when the last one is full.
- So each user has at most one half-empty page. Almost no waste!
- When a user leaves, their pages are freed and given to someone new. โป๏ธ
Result in the lecture's example: about 164 chats fit on one H100.
9. ๐ Trick #2: Continuous batching โ
โ Static batching: wait for the slowest โ
Like a bus ๐ that only leaves when every passenger has reached the last stop:
โ Continuous batching: fill a seat the moment it's free โ
The engine checks after every token step: "Who finished? Who is waiting? Swap them in." No idle seats.
10. ๐ณ Trick #3: RadixAttention / prefix caching (SGLang) โ
The idea: don't redo the same work โ
A company chatbot gives every user the same long system prompt. Why compute its KV cache again for every user?
- If users share the same beginning (prefix), compute its K and V once and reuse it.
- SGLang stores these shared beginnings in a tree (a radix tree), so any matching start can be found quickly.
- Huge win for coding agents: the giant project prompt stays the same while you only ask "make this button blue".
๐ค "But LLMs are stateless!"
True: the model still uses all the old keys and values for every new token. RadixAttention just means they're reused, not recomputed.
11. ๐ฐ The full cost math: naive vs vLLM/SGLang โ
Same goal: 10 billion tokens per day on H100s at $2.5/hour.
Notice: each step is slower (16 ms vs 6.2 ms), but it makes 8ร more tokens per step. That's batching paying off.
| ๐ข Simple server | ๐ vLLM / SGLang | |
|---|---|---|
| Users at once | 16 | 128 |
| Tokens per second per GPU | ~1,300 | ~8,000 |
| Tokens per hour | ~4.7 M | ~29 M |
| Cost per 1 M tokens | ~$0.54 | ~$0.09 |
| Tokens per GPU per day | ~112 M | ~690 M |
| GPUs for 10 B tokens/day | ~90 | ~15 |
| Yearly GPU bill | ~$2.0 M | ~$0.33 M |
๐ About 6ร cheaper, same model, same GPUs, same answers. Only the software changed.
๐งฎ Show the full arithmetic
Simple server
- 1,300 tok/s ร 3,600 s = 4.68 M tokens/hour โ $2.5 รท 4.68 = $0.53 per M tokens
- 1,300 ร 86,400 s = 112 M tokens/day โ 10 B รท 112 M โ 89 โ 90 GPUs
- 90 ร $2.5 ร 24 ร 365 โ $1.97 M/year
vLLM / SGLang
- 8,000 tok/s ร 3,600 = 28.8 M tokens/hour โ $2.5 รท 28.8 = $0.087 per M tokens
- 8,000 ร 86,400 = 691 M tokens/day โ 10 B รท 691 M โ 14.5 โ 15 GPUs
- 15 ร $2.5 ร 24 ร 365 โ $0.33 M/year
The lecturer noted these are rough, back-of-the-envelope numbers. A simple server could squeeze in a few more users, and vLLM could fit ~256. Both sides would shift, but the gap stays about the same.
12. ๐ญ Beyond this lecture โ
vLLM and SGLang do much more than these three tricks, for example:
- Quantization: store weights in fewer bits, so there's less to carry. ๐
- Speculative decoding: guess several tokens cheaply, then check them in one go.
๐ For a deeper dive into these and more, see my Inference Optimization notes.
โ Cheat sheet (remember these) โ
๐ 10 things to remember
- Serving costs more than training over a model's life.
- Llama 8B = 8 B weights ร 2 bytes = 16 GB.
- GPU = big slow VRAM (80 GB) + tiny fast SRAM (~30 MB).
- Carrying data is the bottleneck, not maths (4.8 ms vs 0.016 ms).
- Prefill reads the question in parallel; decode writes one token at a time.
- KV cache saves K and V (not Q) so old tokens aren't recomputed. ~128 KB/token for Llama 8B.
- Batching = load weights once, serve many users.
- PagedAttention = small pages placed anywhere โ ~96% memory used.
- Continuous batching = a finished user's seat is refilled right away.
- RadixAttention = shared prompt beginnings computed once and reused.
